import request from '@/utils/request'
import type {
  FileInfo,
  UploadFileRequest,
  UploadBatchFilesRequest,
  UploadFileResponse,
  FileListResponse,
  TrainingPhoto,
  UploadTrainingPhotoRequest,
  TrainingPhotoListResponse,
  CoachCertificate,
  UploadCoachCertificateRequest,
  CoachCertificateListResponse,
  VerifyCertificateRequest
} from '@/types/api/file'
import type { BaseResponse, PaginationResponse } from '@/types/common/response'

/**
 * 文件上传相关API
 */
export const fileApi = {
  /**
   * 上传单个文件
   * @param data 文件上传信息
   */
  uploadFile(data: UploadFileRequest) {
    const formData = new FormData()
    formData.append('file', data.file)
    formData.append('category', data.category)
    if (data.relatedId !== undefined) {
      formData.append('related_id', data.relatedId.toString())
    }
    if (data.isPublic !== undefined) {
      formData.append('is_public', data.isPublic.toString())
    }

    return request.post<BaseResponse<UploadFileResponse>>('/files/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  /**
   * 批量上传文件
   * @param data 批量文件上传信息
   */
  uploadBatchFiles(data: UploadBatchFilesRequest) {
    const formData = new FormData()
    data.files.forEach(file => {
      formData.append('files', file)
    })
    formData.append('category', data.category)
    if (data.relatedId !== undefined) {
      formData.append('related_id', data.relatedId.toString())
    }
    if (data.isPublic !== undefined) {
      formData.append('is_public', data.isPublic.toString())
    }

    return request.post<BaseResponse<UploadFileResponse[]>>('/files/upload-batch', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  /**
   * 删除文件
   * @param id 文件ID
   */
  deleteFile(id: number) {
    return request.delete<BaseResponse<null>>(`/files/${id}`)
  },

  /**
   * 获取文件列表
   * @param params 查询参数
   */
  getFileList(params: {
    category?: 'AVATAR' | 'CERTIFICATE' | 'TRAINING' | 'COURSE' | 'ANNOUNCEMENT' | 'OTHER'
    fileType?: 'image' | 'video' | 'document'
    page?: number
    pageSize?: number
  }) {
    return request.get<BaseResponse<PaginationResponse<FileInfo>>>('/files', { params })
  }
}

/**
 * 头像管理相关API
 */
export const avatarApi = {
  /**
   * 上传头像
   * @param file 头像文件
   * @param cropX 裁剪起点X
   * @param cropY 裁剪起点Y
   * @param cropWidth 裁剪宽度
   * @param cropHeight 裁剪高度
   */
  uploadAvatar(file: File, cropX?: number, cropY?: number, cropWidth?: number, cropHeight?: number) {
    const formData = new FormData()
    formData.append('file', file)
    if (cropX !== undefined) {
      formData.append('crop_x', cropX.toString())
    }
    if (cropY !== undefined) {
      formData.append('crop_y', cropY.toString())
    }
    if (cropWidth !== undefined) {
      formData.append('crop_width', cropWidth.toString())
    }
    if (cropHeight !== undefined) {
      formData.append('crop_height', cropHeight.toString())
    }

    return request.post<BaseResponse<{ avatarUrl: string }>>('/files/avatar', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}

/**
 * 训练照片管理相关API
 */
export const trainingPhotoApi = {
  /**
   * 上传训练照片
   * @param data 训练照片信息
   */
  uploadTrainingPhoto(data: UploadTrainingPhotoRequest) {
    const formData = new FormData()
    formData.append('file', data.file)
    formData.append('photo_date', data.photoDate)
    if (data.bodyPart) {
      formData.append('body_part', data.bodyPart)
    }
    if (data.weight !== undefined) {
      formData.append('weight', data.weight.toString())
    }
    if (data.bodyFat !== undefined) {
      formData.append('body_fat', data.bodyFat.toString())
    }
    if (data.description) {
      formData.append('description', data.description)
    }
    if (data.isPublic !== undefined) {
      formData.append('is_public', data.isPublic.toString())
    }

    return request.post<BaseResponse<{ id: number; fileUrl: string; photoDate: string; weight?: number; bodyFat?: number }>>('/training-photos', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  /**
   * 获取训练照片列表
   * @param params 查询参数
   */
  getTrainingPhotoList(params: {
    memberId?: number
    startDate?: string
    endDate?: string
    page?: number
    pageSize?: number
  }) {
    return request.get<BaseResponse<TrainingPhotoListResponse>>('/training-photos', { params })
  },

  /**
   * 删除训练照片
   * @param id 照片ID
   */
  deleteTrainingPhoto(id: number) {
    return request.delete<BaseResponse<null>>(`/training-photos/${id}`)
  }
}

/**
 * 教练证书管理相关API
 */
export const coachCertificateApi = {
  /**
   * 上传教练证书
   * @param data 证书信息
   */
  uploadCoachCertificate(data: UploadCoachCertificateRequest) {
    const formData = new FormData()
    formData.append('file', data.file)
    formData.append('certificate_name', data.certificateName)
    if (data.certificateNo) {
      formData.append('certificate_no', data.certificateNo)
    }
    if (data.issueDate) {
      formData.append('issue_date', data.issueDate)
    }
    if (data.expireDate) {
      formData.append('expire_date', data.expireDate)
    }
    if (data.issuingAuthority) {
      formData.append('issuing_authority', data.issuingAuthority)
    }

    return request.post<BaseResponse<{ id: number; certificateName: string; status: string }>>('/coach-certificates', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  /**
   * 获取教练证书列表
   * @param params 查询参数
   */
  getCoachCertificateList(params: {
    coachId?: number
    status?: 'PENDING' | 'APPROVED' | 'REJECTED'
    page?: number
    pageSize?: number
  }) {
    return request.get<BaseResponse<CoachCertificateListResponse>>('/coach-certificates', { params })
  },

  /**
   * 审核教练证书
   * @param id 证书ID
   * @param data 审核信息
   */
  verifyCoachCertificate(id: number, data: VerifyCertificateRequest) {
    return request.put<BaseResponse<null>>(`/coach-certificates/${id}/verify`, data)
  },

  /**
   * 删除教练证书
   * @param id 证书ID
   */
  deleteCoachCertificate(id: number) {
    return request.delete<BaseResponse<null>>(`/coach-certificates/${id}`)
  }
}
